<?php

namespace app\admin\model;

use think\Model;
use think\Db;
use think\Request;
use think\Exception;//事务处理

class Container_carrier extends Model
{
    //承运商列表
    public function getcarriers($res)
    {
        $langs = $where['a.lang'] = $res['lang'];//语言标志
        $join = [
            ['nt_container_carrier b', 'a.mkey=b.mkey']
        ];
        $table = 'multi_lang';
        $pagelimit['limit'] = $res['pageSize'];
        $pagelimit['start'] = ($res['page'] - 1) * $res['pageSize'];
        $datares = getmultidata($table, $join, $where, $pagelimit);
        $data['data'] = $datares['data'];
        $data['count'] = $datares['count'];
        return $data;
    }

    //承运商的添加与编辑
    public function addeditcarrier($data)
    {
        if ($data['act'] == 'carrieradd') {
            $ckdata = notEmpty(['carriername' => lang("carrier_name")]);
        } else {
            $dname_zh = trim($data['lang_zh']);//中文
            $dname_en = trim($data['lang_en']);//英文
            $dname_pt = trim($data['lang_pt']);//葡萄牙文
            $ckdata = notEmpty(['lang_zh' => lang("carrier_name_in_Chinese"), 'lang_en' => lang("carrier_name_in_English"), 'lang_pt' => lang("carrier_name_in_Portuguese")]);
        }
        if ($ckdata['status']==2) {
            return $ckdata;
        }
        $lang = $data['lang'];
        unset($data['lang']);
        if ($data['act'] == 'carrieradd') {
            $depalike = 'container_carrier';
            $langwhere['mkey'] = array('like', $depalike . '%');
            $carriername = $langwhere['content'] = trim($data['carriername']);
            $langzh = getables('multi_lang', $langwhere);
            if ($langzh) {
                return array('msg' => $carriername . lang("already_exists"), 'status' => 3);
            }
            //权限id的写入
            $data['create_at'] = $data['update_at'] = time();
            $carrierid = $this->strict(false)->insertGetId($data);//承运表
            $carrierkey = 'container_carrier_' . $carrierid;
            //更新数据
            $this->where('id', $carrierid)->setField('mkey', $carrierkey);
            $langdata = getables('language');
            $multidata = array();
            foreach ($langdata as $langdatakey => $langdataval) {
                $multidata[$langdatakey]['content'] = $carriername;
                $multidata[$langdatakey]['mkey'] = $carrierkey;
                $multidata[$langdatakey]['lang'] = $langdataval['code'];
            }
            $langid = Db::name('multi_lang')->insertAll($multidata);//语言表
            if ($carrierid && $langid) {
                Db::commit();
                return array('msg' => $carriername . lang("added_successfully"), 'status' => 1);
            } else {
                return array('msg' => $carriername . lang("add_failed"), 'status' => 0);
            }
        } elseif ($data['act'] == 'carrieredit') {
            switch ($lang) {
                case 'zh':
                    $langedit = $dname_zh;
                    break;
                case 'en':
                    $langedit = $dname_en;
                    break;
                case 'pt':
                    $langedit = $dname_pt;
                    break;
                default:
                    $langedit = $dname_zh;
                    break;
            }
            Db::startTrans();
            $datadepart['update_at'] = time();
            $datadepart['code'] = $data['code'];
            $resupid = $this->where('id', $data['id'])->update($datadepart);//更新承运表
            //更新语言表
            $departmkey = 'container_carrier_' . $data['id'];
            $langupidzh = Db::name('multi_lang')->where(['mkey' => $departmkey, 'lang' => 'zh'])->setField('content', $dname_zh);//更新中文承运名称
            $langupiden = Db::name('multi_lang')->where(['mkey' => $departmkey, 'lang' => 'en'])->setField('content', $dname_en);//更新英文承运名称
            $langupidpt = Db::name('multi_lang')->where(['mkey' => $departmkey, 'lang' => 'pt'])->setField('content', $dname_pt);//更新葡文承运名称
            if ($resupid !== false && $langupidzh !== false && $langupiden !== false && $langupidpt !== false) {
                Db::commit();
                return array('msg' => $langedit . lang("edited_successfully"), 'status' => 1);
            } else {
                return array('msg' => $langedit . lang("edit_failed"), 'status' => 0);
            }
        }
    }

    //承运商的删除
    public function delcarrier($data)
    {
        $containerinfo=Db::name('container')->where('carrier_id',$data['id'])->find();
        if ($containerinfo){
            return array('msg' =>  $data['name'] . lang("contains_container_and_can't_be_deleted"), 'status' => 0);
        }
        Db::startTrans();
        $retcarrier = $this->where('id', $data['id'])->delete();//承运表
        $langdel = Db::name('multi_lang')->where('mkey', 'container_carrier_' . $data['id'])->delete();//语言表
        if ($retcarrier && $langdel) {
            Db::commit();
            return array('msg' => $data['name'] . lang("deleted_successfully"), 'status' => 1);
        } else {
            return array('msg' =>  $data['name'] . lang("failed_to_delete"), 'status' => 0);
        }
    }
}